package com.dumian.apple.FilePersistenceTest;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class MainActivity extends AppCompatActivity {
    private EditText mEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEditText= (EditText) findViewById(R.id.main_et);
        /**
         * 文件中读取数据 ->还原显示保存的数据
         * */

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        String inputText=mEditText.getText().toString();
        save(inputText);
    }

    private void save(String inputText) {
        FileOutputStream out=null;
        BufferedWriter writer=null;

        try{
            out=openFileOutput("data", Context.MODE_PRIVATE);
            writer=new BufferedWriter(new OutputStreamWriter(out));
            writer.write(inputText);

        } catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                if(writer != null){
                    writer.close();
                }
            }catch(IOException e){
                e.printStackTrace();
                }

            }
        }
    }


